package com.chapter.sample.jvm.gc;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


/**
 * 3、OOM案例 - GC overhead limit exceeded
 *   -Xlog:gc*  -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/Library/dev/sample/chapter-common-platform/chapter-common-sample/heap/heapdump.hprof -Xms10m -Xmx10m -Xloggc:/Library/dev/sample/chapter-common-platform/chapter-common-sample/log/gc.log -XX:+UseParallelGC
 */
public class OOMTest {
    public static void main(String[] args) {
        test1();
    }

    //异常：overhead limit exceeded
    public static void test1() {
        int i = 0;
        List<String> list = new ArrayList<>();
        try {
            while (true) {
                list.add(UUID.randomUUID().toString().intern());
            }
        } catch (Throwable e) {
            System.out.println("*****************i" + i);
            e.printStackTrace();
            throw e;
        }
    }

    //异常：java heap space
    public static void test2() {
        String str = "";
        Integer i = 1;
        try {
            while (true) {
                i++;
                str += UUID.randomUUID();
            }
        } catch (Throwable e) {
            System.out.println("*****************i" + i);
            e.printStackTrace();
            throw e;
        }
    }
}
